<?php
/**
 * Created by PhpStorm.
 * User: wanglei
 * Date: 2016/7/27
 * Time: 20:59
 * 行业三级联动管理
 */
defined('IN_PHPCMS') or exit('No permission resources.');

pc_base::load_app_class('admin','admin',0);
pc_base::load_sys_class('form','',0);
pc_base::load_sys_class('format','',0);

class industry extends admin {

    /**
     * 首页显示
     */
    public function init()
    {

        $shuafen_industry_model = pc_base::load_model("shuafen_industry_model");
        $industry_data = $shuafen_industry_model->select("", '*', '',   'pid asc');
        $industry_data = $this->getTree($industry_data);
        include $this->admin_tpl('industry/list');
    }
    function getTree($menus){
        $items = array();
        foreach($menus as $menu){
            $items[$menu['id']] = $menu;
        }
        foreach($items as $key=>$item){
            $items[$item['pid']]['son'][$item['id']]=&$items[$item['id']];
        }
        return isset($items[0]['son'])?$items[0]['son']:array();
    }
    /**
     * 修改
     */
    public function edit()
    {

        $shuafen_industry_model = pc_base::load_model("shuafen_industry_model");
        if (isset($_POST['dosubmit'])) {
            $industry_name = $_POST['industry_name'];
            $id = $_POST['id'];
            $result = $shuafen_industry_model->update(array('industry_name'=>$industry_name),array('id'=>$id));
            if($result){
                showmessage('修改成功', 'index.php?m=shuafen&c=industry&a=init');
            }else{
                showmessage('修改失败', 'index.php?m=shuafen&c=industry&a=ini');
            }
        }else{
            $id = $_GET['id'];
            $data = $shuafen_industry_model->get_one(array('id'=>$id));
            include $this->admin_tpl('industry/edit');
        }
    }
    /**
     * 添加
     */
    public function add()
    {
        $shuafen_industry_model = pc_base::load_model("shuafen_industry_model");
        if (isset($_POST['dosubmit'])) {
            $industry_name = $_POST['industry_name'];
            $pid = $_POST['pid'];
            $result = $shuafen_industry_model->insert(array('pid'=>$pid,'industry_name'=>$industry_name));
            if($result){
                showmessage('添加成功', 'index.php?m=shuafen&c=industry&a=init');
            }else{
                showmessage('添加失败', 'index.php?m=shuafen&c=industry&a=init');
            }
        }else{
            $pid = $_GET['pid'];
            if(!$pid){
                $pid_name ='一级';
            }else{
                $pid_name ='('.$_GET['pid_name'].')的子';

            }
            include $this->admin_tpl('industry/add');
        }
    }
    /**
     * 删除
     */
    public function del()
    {
        $id = intval($_GET['id']);
        $shuafen_industry_model = pc_base::load_model("shuafen_industry_model");
        $result = $shuafen_industry_model->delete("id=".$id." or pid=".$id);
        echo json_encode(array('status'=>$result));
    }

}
